﻿#include "lvgl_bilibili.h"
//#include "lvgl_Project.h"
#include "lvgl_bilibili_anim.h"
#include "lvgl_image.h"


_lvgl_bilibili_data lvgl_bilibili_data ;
// = {
//     .cont_main = NULL,
//     .task = NULL,
//     .anim_in = NULL,
//     .anim_out = NULL,
//     .anim_path_in = NULL,
//     .anim_path_out = NULL,
// };

void lvgl_bilibili_set_tou_bmp(int num);

void lvgl_bilibili_TaskCb(lv_task_t* t)
{
    static int bmp_time = 0, bmp_cnt = 0, bmp_flag = 0, time = 0;

    if (bmp_flag == 0)
    {
        if (++bmp_time > 1)
        {
            bmp_time = 0;
            if (++bmp_cnt > 3)
            {
                bmp_cnt = 0;
                bmp_flag = 1;
            }
            lvgl_bilibili_set_tou_bmp(bmp_cnt);
        }
    }
    else
    {
        if (++bmp_time > 50 * 2)
        {
            bmp_time = 0;
            bmp_flag = 0;
            bmp_cnt = 0;
        }
    }

#if USE_WIN
    if (system_data.HuoQu_bilibili_Flag == true && lvgl_refresh_get_sta() == lvgl_refresh_Zhong)
    {
        if (++time > 100)
        {
            time = 0;
            lvgl_refresh_close();
        }
    }
#endif

}

const lv_img_dsc_t* bmp_gif[] =
{
    &bmp_bilibili_01_01,
    &bmp_bilibili_01_02,
    &bmp_bilibili_01_03,
    &bmp_bilibili_01_01,
};
//切换头部图片
void lvgl_bilibili_set_tou_bmp(int num)
{

    lv_img_set_src(lvgl_bilibili_data.lvgl_tou_image, bmp_gif[num]);
    //lv_obj_invalidate(lvgl_bilibili_data.lvgl_tou_image);
}

//设置粉丝
void lvgl_bilibili_Set_fensi(char* str)
{
    char buf[20];
    //if (lvgl_bilibili_data.lvgl_cont_fensi == NULL)
    {
        //----创建容器----//
        lvgl_bilibili_data.lvgl_cont_fensi = lv_cont_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_cont_fensi, 16, 56);
        lv_obj_set_size(lvgl_bilibili_data.lvgl_cont_fensi, 214, 32);
        lv_obj_set_drag_parent(lvgl_bilibili_data.lvgl_cont_fensi, true); //启用 / 禁用父对象可拖动
        lv_obj_add_style(lvgl_bilibili_data.lvgl_cont_fensi, LV_OBJ_PART_MAIN, &lvgl_style.cont_transparent);
        lv_obj_align(lvgl_bilibili_data.lvgl_cont_fensi, NULL, LV_ALIGN_IN_TOP_MID, 0, 50);

        lvgl_bilibili_data.lvgl_Label_biaoti_fensi = lv_label_create(lvgl_bilibili_data.lvgl_cont_fensi, NULL);
        lv_label_set_long_mode(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, LV_LABEL_LONG_SROLL_CIRC);//循环滚动
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, 0, 0);
        lv_obj_set_width(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, 102);
        lv_obj_set_height(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, 32);
        lv_label_set_recolor(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, true);
        lv_label_set_align(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, LV_LABEL_ALIGN_RIGHT);
        lv_label_set_recolor(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, true);
        lv_label_set_text(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, lvgl_globa_text[17][system_get_Language()]);

        lv_obj_add_style(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, LV_OBJ_PART_MAIN, &lvgl_style.font_changyong_16_white_bg);

        lv_label_set_anim_speed(lvgl_bilibili_data.lvgl_Label_biaoti_fensi, 20);


        lvgl_bilibili_data.lvgl_Label_fensi = lv_label_create(lvgl_bilibili_data.lvgl_cont_fensi, lvgl_bilibili_data.lvgl_Label_biaoti_fensi);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_Label_fensi, 104, 0);
        lv_obj_set_width(lvgl_bilibili_data.lvgl_Label_fensi, 110);
        lv_obj_set_height(lvgl_bilibili_data.lvgl_Label_fensi, 32);
        lv_label_set_align(lvgl_bilibili_data.lvgl_Label_fensi, LV_LABEL_ALIGN_LEFT);

    }
    if (str == NULL)
    {

        lv_label_set_text(lvgl_bilibili_data.lvgl_Label_fensi, "----");
        return;
    }

    lv_label_set_text(lvgl_bilibili_data.lvgl_Label_fensi, str);
}

//设置播放量
void lvgl_bilibili_Set_bofang(char* str)
{
    char buf[20];
    //if (lvgl_bilibili_data.lvgl_cont_bofang == NULL)
    {
        //----创建容器----//
        lvgl_bilibili_data.lvgl_cont_bofang = lv_cont_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_cont_bofang, 16, 97);
        lv_obj_set_size(lvgl_bilibili_data.lvgl_cont_bofang, 214, 32);
        lv_obj_set_drag_parent(lvgl_bilibili_data.lvgl_cont_bofang, true); //启用 / 禁用父对象可拖动
        lv_obj_add_style(lvgl_bilibili_data.lvgl_cont_bofang, LV_OBJ_PART_MAIN, &lvgl_style.cont_transparent);

        lv_obj_align(lvgl_bilibili_data.lvgl_cont_bofang, NULL, LV_ALIGN_IN_TOP_MID, 0, 75);

        lvgl_bilibili_data.lvgl_Label_biaoti_bofang = lv_label_create(lvgl_bilibili_data.lvgl_cont_bofang, NULL);
        lv_label_set_long_mode(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, LV_LABEL_LONG_SROLL_CIRC);//循环滚动
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, 0, 0);
        lv_obj_set_width(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, 102);
        lv_obj_set_height(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, 32);
        lv_label_set_recolor(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, true);
        lv_label_set_align(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, LV_LABEL_ALIGN_RIGHT);
        lv_label_set_recolor(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, true);
        lv_label_set_text(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, lvgl_globa_text[18][system_get_Language()]);

        lv_obj_add_style(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, LV_OBJ_PART_MAIN, &lvgl_style.font_changyong_16_white_bg);

        lv_label_set_anim_speed(lvgl_bilibili_data.lvgl_Label_biaoti_bofang, 20);


        lvgl_bilibili_data.lvgl_Label_bofang = lv_label_create(lvgl_bilibili_data.lvgl_cont_bofang, lvgl_bilibili_data.lvgl_Label_biaoti_bofang);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_Label_bofang, 104, 0);
        lv_obj_set_width(lvgl_bilibili_data.lvgl_Label_bofang, 110);
        lv_obj_set_height(lvgl_bilibili_data.lvgl_Label_bofang, 32);
        lv_label_set_align(lvgl_bilibili_data.lvgl_Label_bofang, LV_LABEL_ALIGN_LEFT);

    }
    if (str == NULL)
    {

        lv_label_set_text(lvgl_bilibili_data.lvgl_Label_bofang, "----");
        return;
    }

    lv_label_set_text(lvgl_bilibili_data.lvgl_Label_bofang, str);
}


//设置关注
void lvgl_bilibili_Set_guanzhu(char* str)
{
    char buf[20];
    //if (lvgl_bilibili_data.lvgl_cont_guanzhu == NULL)
    {
        //----创建容器----//
        lvgl_bilibili_data.lvgl_cont_guanzhu = lv_cont_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_cont_guanzhu, 16, 138);
        lv_obj_set_size(lvgl_bilibili_data.lvgl_cont_guanzhu, 214, 32);
        lv_obj_set_drag_parent(lvgl_bilibili_data.lvgl_cont_guanzhu, true); //启用 / 禁用父对象可拖动
        lv_obj_add_style(lvgl_bilibili_data.lvgl_cont_guanzhu, LV_OBJ_PART_MAIN, &lvgl_style.cont_transparent);
        lv_obj_align(lvgl_bilibili_data.lvgl_cont_guanzhu, NULL, LV_ALIGN_IN_TOP_MID, 0, 100);

        lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu = lv_label_create(lvgl_bilibili_data.lvgl_cont_guanzhu, NULL);
        lv_label_set_long_mode(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, LV_LABEL_LONG_SROLL_CIRC);//循环滚动
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, 0, 0);
        lv_obj_set_width(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, 102);
        lv_obj_set_height(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, 32);
        lv_label_set_recolor(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, true);
        lv_label_set_align(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, LV_LABEL_ALIGN_RIGHT);
        lv_label_set_recolor(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, true);
        lv_label_set_text(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, lvgl_globa_text[19][system_get_Language()]);

        lv_obj_add_style(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, LV_OBJ_PART_MAIN, &lvgl_style.font_changyong_16_white_bg);

        lv_label_set_anim_speed(lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu, 20);


        lvgl_bilibili_data.lvgl_Label_guanzhu = lv_label_create(lvgl_bilibili_data.lvgl_cont_guanzhu, lvgl_bilibili_data.lvgl_Label_biaoti_guanzhu);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_Label_guanzhu, 104, 0);
        lv_obj_set_width(lvgl_bilibili_data.lvgl_Label_guanzhu, 110);
        lv_obj_set_height(lvgl_bilibili_data.lvgl_Label_guanzhu, 32);
        lv_label_set_align(lvgl_bilibili_data.lvgl_Label_guanzhu, LV_LABEL_ALIGN_LEFT);

    }
    if (str == NULL)
    {

        lv_label_set_text(lvgl_bilibili_data.lvgl_Label_guanzhu, "----");
        return;
    }

    lv_label_set_text(lvgl_bilibili_data.lvgl_Label_guanzhu, str);
}


void lvgl_bilibili_button_cb(struct _lv_obj_t* obj, lv_event_t event)
{
    int i = 0;
    //printf("cb:%d", event);
    switch (event)
    {

    case LV_EVENT_PRESSING://对象被连续按下

        break;
    case LV_EVENT_PRESSED://对象已被按下


        break;


    case LV_EVENT_CLICKED://非拖动下释放

        break;





    case LV_EVENT_RELEASED://按钮释放


        break;


    case bilibili_event_anXia:

        if (0)//obj == lvgl_bilibili_Button[0])
        {
            lvgl_set_obj_hide(&lvgl_bilibili_data.cont_main);
            //lvgl_TianQi_create(lv_scr_act());
        }
        else
        {
            //lvgl_bilibili_anim_btn_item_jin();

        }

        break;

    case LV_EVENT_LONG_PRESSED://按钮长按



        break;
    default:
        break;

    }

}







void lvgl_bilibili_event_cb(struct _lv_obj_t* obj, lv_event_t event)
{
    if (obj == lvgl_bilibili_data.cont_main)
    {
        //lvgl_bilibili_debug("cb:%d", event);
        switch (event)
        {

        case LV_EVENT_KEY:

            switch (*(const uint32_t*)lv_event_get_data())
            {
            case LV_KEY_UP:
                lvgl_bilibili_debug("LV_KEY_UP");

                break;
            case LV_KEY_DOWN:
                lvgl_bilibili_debug("LV_KEY_DOWN");

                break;
            case LV_KEY_LEFT:
                lvgl_bilibili_debug("LV_KEY_LEFT");


                break;
            case LV_KEY_RIGHT:
                lvgl_bilibili_debug("LV_KEY_RIGHT");


                break;
            case LV_KEY_ESC:
                lvgl_bilibili_debug("LV_KEY_ESC");

                if (lvgl_bilibili_data.refresh_Sta == lvgl_refresh_Zhong)
                {
                    break;
                }
                lvgl_bilibili_anim_out_start(0, 0);


                break;
            case LV_KEY_DEL:
                lvgl_bilibili_debug("LV_KEY_DEL");

                break;
            case LV_KEY_BACKSPACE:
                lvgl_bilibili_debug("LV_KEY_BACKSPACE");

                break;
            case LV_KEY_ENTER:
                lvgl_bilibili_debug("LV_KEY_ENTER");


                if (lvgl_bilibili_data.refresh_flag == false)
                {
                    lvgl_bilibili_data.refresh_flag = true;
                    lvgl_bilibili_shuaxin_kaishi();
                }



                break;
            case LV_KEY_NEXT:
                lvgl_bilibili_debug("LV_KEY_NEXT");

                break;
            case LV_KEY_PREV:
                lvgl_bilibili_debug("LV_KEY_PREV");

                break;
            case LV_KEY_HOME:
                lvgl_bilibili_debug("LV_KEY_HOME");

                break;
            case LV_KEY_END:
                lvgl_bilibili_debug("LV_KEY_END");

                break;

            }

            break;

        case CZ_EVENT_REFRESH_OVER:
            lvgl_bilibili_shuaxin_jieshu();
            break;
        case CZ_EVENT_ANIM_IN_READY:

            if (lvgl_bilibili_data.refresh_first_flag == false)
            {
                lvgl_bilibili_data.refresh_first_flag = true;
                uint32_t user_data = LV_KEY_ENTER;
                lvgl_send_event(&lvgl_bilibili_data.cont_main, LV_EVENT_KEY, &user_data);
            }


            break;
        case LV_EVENT_DELETE:

            lvgl_bilibili_data.cont_main = NULL;

            lvgl_bilibili_debug("delete win");

            break;

        case LV_EVENT_PRESSING://对象被持续按下


            break;
        case LV_EVENT_PRESSED://对象已被按下


            break;
        case LV_EVENT_DRAG_END://拖动结束后


            break;

        case LV_EVENT_RELEASED://按钮释放

            break;
        case LV_EVENT_LONG_PRESSED://按钮长按

            break;

        default:
            break;

        }

    }
}


void lvgl_bilibili_create(lv_obj_t* parent)
{

    if (lvgl_bilibili_data.cont_main == NULL)
    {
        //----创建容器----//
        lvgl_bilibili_data.cont_main = lv_cont_create(parent, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.cont_main, 0, -(lv_obj_get_height(parent)));
        lv_obj_set_size(lvgl_bilibili_data.cont_main, lv_obj_get_width(parent), lv_obj_get_height(parent));
        //lv_obj_set_drag_parent(lvgl_bilibili_data.cont_main, true); //启用 / 禁用父对象可拖动

        lv_obj_set_click(lvgl_bilibili_data.cont_main, false); //启用 / 禁用可点击
        lv_obj_set_drag(lvgl_bilibili_data.cont_main, false);//启用/禁用对象可拖动
        //lv_obj_set_drag_dir(lvgl_bilibili_data.cont_main, LV_DRAG_DIR_ONE);//设置拖动方向
        // lv_obj_set_drag_throw(lvgl_bilibili_data.cont_main, false);//启用/禁用拖动后是否有惯性移动

        lv_obj_add_style(lvgl_bilibili_data.cont_main, LV_OBJ_PART_MAIN, &lvgl_style.cont_no_border);//设置样式
        lv_obj_set_event_cb(lvgl_bilibili_data.cont_main, lvgl_bilibili_event_cb);//设置回调函数


        //----文字显示----//
#if USE_WIN
        lvgl_bilibili_Set_fensi("66666");
        lvgl_bilibili_Set_bofang("66666");
        lvgl_bilibili_Set_guanzhu("888888");
#else
        lvgl_bilibili_Set_fensi(system_data.bilibili_Data.fensi);
        lvgl_bilibili_Set_bofang(system_data.bilibili_Data.bofang);
        lvgl_bilibili_Set_guanzhu(system_data.bilibili_Data.guanzhu);
#endif

        //----文字显示----//

        //----头部图片----//
        lvgl_bilibili_data.lvgl_tou_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_tou_image, 0, 0);
        lv_img_set_src(lvgl_bilibili_data.lvgl_tou_image, &bmp_bilibili_01_01);
        //----头部图片----//

        //----logo图片----//
        lvgl_bilibili_data.lvgl_logo_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_logo_image, 30, lv_obj_get_height(parent) - 44);
        lv_img_set_src(lvgl_bilibili_data.lvgl_logo_image, &bmp_bilibili_03_03);
        lv_obj_align(lvgl_bilibili_data.lvgl_logo_image, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
        //----logo图片----//

        //----叶子图片----//
        lvgl_bilibili_data.lvgl_yezi_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_yezi_image, lv_obj_get_width(lvgl_bilibili_data.cont_main), 10);
        lv_img_set_src(lvgl_bilibili_data.lvgl_yezi_image, &bmp_bilibili_04_01);
        //----叶子图片----//


        //----头 前图层图片----//

        lvgl_bilibili_data.lvgl_tou_qian_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_tou_qian_image, 0, 1);
        lv_img_set_src(lvgl_bilibili_data.lvgl_tou_qian_image, &bmp_bilibili_02_01);
        //----头 前图层图片----//


        //----左侧图片----//
        lvgl_bilibili_data.lvgl_zuo_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_img_set_src(lvgl_bilibili_data.lvgl_zuo_image, &bmp_bilibili_03_01);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_zuo_image, -(lv_obj_get_width(lvgl_bilibili_data.lvgl_zuo_image)), 80);
        //----左侧图片----//

        //----右侧图片----//
        lvgl_bilibili_data.lvgl_you_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_img_set_src(lvgl_bilibili_data.lvgl_you_image, &bmp_bilibili_03_02);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_you_image, lv_obj_get_width(lv_scr_act()), 80);
        //----右侧图片----//


        //----底 前图层图片----//
        lvgl_bilibili_data.lvgl_qian_image = lv_img_create(lvgl_bilibili_data.cont_main, NULL);
        lv_obj_set_pos(lvgl_bilibili_data.lvgl_qian_image, 0, lv_obj_get_height(parent) - 36);
        lv_img_set_src(lvgl_bilibili_data.lvgl_qian_image, &bmp_bilibili_02_02);
        lv_obj_align(lvgl_bilibili_data.lvgl_qian_image, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
        //----底 前图层图片----//

        lvgl_bilibili_debug("create win");
    }
    else
    {

        lvgl_bilibili_debug("show win");
    }


    lv_obj_move_background(lvgl_bilibili_data.cont_main);
    lvgl_set_obj_show(&lvgl_bilibili_data.cont_main);

    lvgl_task_create(&lvgl_bilibili_data.task, lvgl_bilibili_TaskCb, 20, LV_TASK_PRIO_HIGH, NULL);

    lvgl_bilibili_anim_in_start(0, 0);
    lvgl_bilibili_anim_Jin();

}

void lvgl_bilibili_close(void)
{
    lvgl_bilibili_debug("close");

    lvgl_group_remove_obj(&lvgl_bilibili_data.cont_main);

    lvgl_task_delete(&lvgl_bilibili_data.task);

    lvgl_obj_clean(&lvgl_bilibili_data.cont_main);
    lvgl_obj_del(&lvgl_bilibili_data.cont_main);

    //lvgl_set_obj_hide(&lvgl_bilibili_data.cont_main);

    lvgl_desktop_create(lv_scr_act());

}


void lvgl_bilibili_closed(void)
{

    lvgl_bilibili_debug("closed");
    lvgl_bilibili_data.cont_main = NULL;
}



//刷新开始
void lvgl_bilibili_shuaxin_kaishi(void)
{
    if (lvgl_bilibili_data.refresh_Sta != lvgl_refresh_Zhong)
    {
        lvgl_bilibili_debug("start refresh");
        lv_obj_set_drag(lvgl_bilibili_data.cont_main, false);//设置拖动方向
        lvgl_bilibili_data.refresh_Sta = lvgl_refresh_Zhong;

        lvgl_refresh_start(&lvgl_bilibili_data.cont_main, &system_data.HuoQu_bilibili_Flag, 30);
    }
    else
    {
        lvgl_bilibili_debug("refreshing");
    }

}

//刷新结束
void lvgl_bilibili_shuaxin_jieshu(void)
{
    lvgl_bilibili_debug("refresh ready");

    lvgl_bilibili_data.refresh_flag = false;
    lvgl_bilibili_data.refresh_Sta = lvgl_refresh_JieShu;
    system_data.HuoQu_bilibili_Flag = false;
    lv_obj_set_drag_dir(lvgl_bilibili_data.cont_main, LV_DRAG_DIR_ONE);//设置拖动方向
}


